package com.techshinogongjian.fp.client;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.fingerUsb.Callback;
import com.techshinogongjian.fp.comm.MsgCallback;
import com.fingerUsb.FingerResult;
import com.techshinogongjian.fp.func.IFingerprintFunc;
import com.techshinogongjian.fp.util.ExecutorUtil;
import com.techshinogongjian.fp.util.FpConst;

/**
 * 指纹客户端
 *
 * @author wangzhi
 */
public class TcFingerClient {

	IFingerprintFunc mFingerprintFunc;

	Handler mHandler;

	Context mContext;

	public TcFingerClient(IFingerprintFunc fingerprintFunc,Context context) {
		mFingerprintFunc = fingerprintFunc;
		mHandler = new Handler(context.getMainLooper());
	}

	public FingerResult tcGetDevFWInfo() {
		return mFingerprintFunc.tcGetDevFWInfo();
	}

	public int tcDevDetect(int nPort, String sPort) {
		return 0;
	}

	public FingerResult tcGetDevSn() {
		return mFingerprintFunc.tcGetDevSn();
	}

	public FingerResult tcSetBaudRate(int baudRate) {
		return mFingerprintFunc.tcSetBaudRate(baudRate);
	}

	public FingerResult tcGetImage(int nTimeout) {
		return mFingerprintFunc.tcGetImage(nTimeout);
	}

	public FingerResult tcGetFeature(int nTimeout) {
		return mFingerprintFunc.tcGetFeature(nTimeout);
	}

	public FingerResult tcGetTemplate(int nTimeout) {
		return mFingerprintFunc.tcGetTemplate(nTimeout);
	}

	public FingerResult tcBooTL() {
		return mFingerprintFunc.tcBooTL();
	}

	public FingerResult tcSetSecurityLevel(int level) {
		return mFingerprintFunc.tcSetSecurityLevel(level);
	}

	public FingerResult tcMatch(byte[] feature,byte[] template) {
		return mFingerprintFunc.tcMatch(feature, template, 0);
	}

	public FingerResult tcSetEnrollImageCount(int newCount) {
		return mFingerprintFunc.tcSetEnrollImageCount(newCount);
	}

	public int tcCancel() {
		if (isCancel()) {
			return 0;
		}
		mFingerprintFunc.tcCancel();
		return 0;
	}

	public boolean isConnected() {
		return mFingerprintFunc.isConnected();
	}

	public boolean isCancel() {
		return mFingerprintFunc.isCancel();
	}

	public boolean isBusy() {
		return mFingerprintFunc.isBusy();
	}

	public void tcSetBaudRate(final int baudRate,final Callback callback) {
		ExecutorUtil.exec(new Runnable() {

			@Override
			public void run() {
				onResult(tcSetBaudRate(baudRate), callback);
			}
		});
	}

	/**
	 * 异步获取指纹设备信息
	 * @param callback
	 */
	public void tcGetDevFWInfo(final Callback callback) {
		ExecutorUtil.exec(new Runnable() {

			@Override
			public void run() {
				onResult(tcGetDevFWInfo(), callback);
			}
		});
	}

	/**
	 * 异步获取设备SN号
	 * @param callback
	 */
	public void tcGetDevSn(final Callback callback) {
		ExecutorUtil.exec(new Runnable() {

			@Override
			public void run() {
				onResult(tcGetDevSn(), callback);
			}
		});
	}

	/**
	 * 异步获取指纹图像
	 * @param nTimeout
	 * @param callback
	 */
	public void tcGetImage(final int nTimeout, final Callback callback) {
		ExecutorUtil.exec(new Runnable() {

			@Override
			public void run() {
				onResult(tcGetImage(nTimeout), callback);
			}
		});
	}

	/**
	 * 异步获取指纹特征
	 * @param nTimeout
	 * @param callback
	 */
	public void tcGetFeature(final int nTimeout, final Callback callback) {
		ExecutorUtil.exec(new Runnable() {

			@Override
			public void run() {
				onResult(tcGetFeature(nTimeout), callback);
			}
		});
	}

	/**
	 * 异步获取指纹模板数据
	 * @param nTimeout
	 * @param callback
	 */
	public void tcGetTemplate(final int nTimeout, final Callback callback) {
		ExecutorUtil.exec(new Runnable() {

			@Override
			public void run() {
				onResult(tcGetTemplate(nTimeout), callback);
			}
		});
	}

	/**
	 * 异步标定
	 * @param callback
	 */
	public void tcBooTL(final Callback callback) {
		ExecutorUtil.exec(new Runnable() {

			@Override
			public void run() {
				onResult(tcBooTL(), callback);
			}
		});
	}

	/**
	 * 异步设置设备安全等级
	 * @param level
	 * @param callback
	 */
	public void tcSetSecurityLevel(final int level, final Callback callback) {
		ExecutorUtil.exec(new Runnable() {

			@Override
			public void run() {
				onResult(tcSetSecurityLevel(level), callback);
			}
		});
	}

	/**
	 * 异步导入指纹模板
	 * @param fid
	 * @param data
	 * @param callback
	 */
	public void tcSetTemplate(final long fid,final byte[] data, final Callback callback) {
		if (callback == null) {
			return;
		}
		ExecutorUtil.exec(new Runnable() {

			@Override
			public void run() {
				onResult(mFingerprintFunc.tcSetTemplate(fid, data), callback);
			}
		});
	}

	/**
	 * 异步注册指纹
	 * @param callback
	 * @param timeout
	 */
	public void tcEnroll(final Callback callback,final int timeout) {
		ExecutorUtil.exec(new Runnable() {

			@Override
			public void run() {

				onResult(mFingerprintFunc.tcEnroll(new MsgCallback() {

					@Override
					public void call(int code, byte[] param) {
						Log.e("code",code+"===");
						sendMsg(code, param, callback);
					}
				},timeout), callback);
			}
		});
	}

	/**
	 * 异步设置注册指纹数量
	 * @param newCount
	 * @param callback
	 */
	public void tcSetEnrollImageCount(final int newCount,
			final Callback callback) {
		ExecutorUtil.exec(new Runnable() {

			@Override
			public void run() {
				onResult(tcSetEnrollImageCount(newCount), callback);
			}
		});
	}

	/**
	 * 异步认证
	 * @param fid
	 * @param callback
	 * @param timeout
	 */
	public void tcVerify(final long fid, final Callback callback,final int timeout) {
		ExecutorUtil.exec(new Runnable() {

			@Override
			public void run() {
				onResult(mFingerprintFunc.tcVerify(fid, new MsgCallback() {

					@Override
					public void call(int code, byte[] param) {
						sendMsg(code, param, callback);
					}
				},timeout), callback);
			}
		});
	}

	private void onResult(final FingerResult result, final Callback callback) {
		Log.e("TcFingerClient","onResult");
		if (callback == null) {
			return;
		}
		Log.e("TcFingerClient","callback");
		if (result == null) {
			mHandler.post(new Runnable() {

				@Override
				public void run() {
					callback.onError(FpConst.TCY_NLNK);
				}
			});
			return;
		}
		Log.e("TcFingerClient","result");
		int status = result.getStatus();
		Log.e("TcFingerClient","status:"+status);
		if (status >= 0) {
			mHandler.post(new Runnable() {
				@Override
				public void run() {
					Log.e("mHandler","status");
					callback.onSuccess(result);
				}
			});
		} else {
			mHandler.post(new Runnable() {
				@Override
				public void run() {
					callback.onError(result.getStatus());
				}
			});
		}
	}

	private void sendMsg(final int code, final byte[] param,
			final Callback callback) {
		if (callback == null) {
			return;
		}
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				callback.onProcess(code, param);
			}
		});
	}
}
